<script setup>
import { ref, onMounted } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { artGetListService, artDelService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import ArticlePreview from './components/ArticlePreview.vue'
// 加载状态
const loading = ref(false)
// 获取表单
const form = ref()
// 获取抽屉
const EditDrawer = ref()
const PreviewDrawer = ref()
// 搜索状态
const isSearch = ref(false)
// 分类id
const cate_id = ref('')
// 文章状态
const state = ref('')
// 文章管理数据
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id,
  state
})
// 文章管理列表
const articleList = ref([])
// 文章管理总数
const total = ref(0)
// 请求文章管理列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
// 搜索
const search = () => {
  isSearch.value = true
  params.value = {
    pagenum: 1,
    pagesize: params.value.pagesize,
    cate_id,
    state
  }
  getArticleList()
}
// 重置
const reset = () => {
  cate_id.value = ''
  state.value = ''
  if (isSearch.value) {
    params.value = {
      pagenum: 1,
      pagesize: params.value.pagesize,
      cate_id,
      state
    }
    isSearch.value = false
    getArticleList()
  }
}

onMounted(() => {
  getArticleList()
})
// 添加文章
const onAddArticle = () => {
  EditDrawer.value.open()
}
// 编辑文章
const onEditArticle = (row) => {
  EditDrawer.value.open(row)
}
// 文章预览
const onPreview = (row) => {
  PreviewDrawer.value.open(row)
}
// 删除文章
const onDelArticle = async (row) => {
  const { data } = await artDelService(row.id)
  ElMessage.success(data.message)
  getArticleList()
}
// 渲染的数量
const onSizeChange = (size) => {
  params.value = {
    pagenum: 1,
    pagesize: size,
    cate_id: isSearch.value ? cate_id : '',
    state: isSearch.value ? state : ''
  }
  getArticleList()
}
// 分页改变
const onCurrentChange = (page) => {
  params.value = {
    pagenum: page,
    pagesize: params.value.pagesize,
    cate_id: isSearch.value ? cate_id : '',
    state: isSearch.value ? state : ''
  }
  getArticleList()
}

const onSuccess = (type) => {
  if (type === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <el-form ref="form">
      <el-form-item label="文章分类：">
        <ChannelSelect v-model="cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select v-model="state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link @click="onPreview(row)" type="primary" :underline="false">{{
            row.title
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            :icon="Edit"
            type="primary"
            plain
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            circle
            :icon="Delete"
            type="danger"
            plain
            @click="onDelArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[3, 5, 7]"
      background
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="justify-content: flex-end"
    />
    <ArticleEdit ref="EditDrawer" @success="onSuccess"></ArticleEdit>
    <ArticlePreview ref="PreviewDrawer"> </ArticlePreview>
  </PageContainer>
</template>

<style lang="scss" scoped></style>
